﻿

Imports Cls_Common
Imports VSModel
Imports System.Data

Partial Class FrontEnd_Control_TinMuaXe_TinMua
    Inherits System.Web.UI.UserControl
    Public strMenu As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            'If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
            '    ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "abc", "ajaxJquery()", True)
            'Else
            '    Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "abc", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            'End If

            'Load theo type
            txtTitle.Text = Request("s")
            LoadTinhThanh()
            TinhThanhChinh()
            BindKhoangGia()


           
            BindToGrid(1, 0)




        End If
    End Sub

    Protected Sub BindKhoangGia()
        Using Data As New VSEntities
            Dim p = (From q In Data.TinMuaXeKhoangGias Select q).ToList
            With ddlKhoangGia
                .DataValueField = "Id"
                .DataTextField = "Title"

                .DataSource = p
                .DataBind()
                .Items.Insert(0, New ListItem("--- Tất cả ---", 0))
            End With
        End Using
    End Sub
    
    
    Protected Sub LoadTinhThanh()
        Using Data As New VSEntities

            Dim p = (From q In Data.KhuVucs Select q).ToList
            For Each a In p
                'insert khu vuc
                Dim itm As New ListItem
                itm.Text = a.Name
                itm.Value = 0
                itm.Attributes.Add("disabled", "disabled")
                itm.Attributes.Add("class", "Khuvuc")
                ddlTinhThanh.Items.Add(itm)

                Dim _khuvucId = a.Id
                Dim k = (From q In Data.DonViHanhChinhs Where q.KhuVucId = _khuvucId Order By q.Title Ascending Select q).ToList
                For Each item In k
                    Dim itm1 As New ListItem
                    itm1.Value = item.Id
                    itm1.Text = "---" & item.Title
                    itm1.Enabled = True
                    ddlTinhThanh.Items.Add(itm1)
                    ddlTinhThanhSearh.Items.Add(itm1)
                Next
            Next
        End Using
    End Sub

    Protected Sub TinhThanhChinh()
        Dim curTinhThanh As New DataTable
        curTinhThanh.Columns.Add("Id")
        curTinhThanh.Columns.Add("Title")

        Using Data As New VSEntities



            'toan quoc here
            Dim _rowToanQuoc = curTinhThanh.NewRow
            _rowToanQuoc(0) = 0

            _rowToanQuoc(1) = "Toàn quốc"
            curTinhThanh.Rows.Add(_rowToanQuoc)


            'main tinh thanh here
            Dim p = (From q In Data.DonViHanhChinhs Where q.MainTinhThanh = True Select New With {.Id = q.Id, .Title = q.Title}).ToList
            Dim k = (From q In Data.DonViHanhChinhs Where q.MainTinhThanh = True Select q.Id).ToList

            For Each a In p
                Dim _row1 = curTinhThanh.NewRow
                _row1(0) = a.Id
                _row1(1) = a.Title
                curTinhThanh.Rows.Add(_row1)
            Next

            Dim selectedTinhThanhId As String = ddlTinhThanh.SelectedValue

            'current choice tinh thanh
            If selectedTinhThanhId <> 0 And Not k.Contains(selectedTinhThanhId) Then
                Dim _row = curTinhThanh.NewRow
                _row(0) = selectedTinhThanhId
                Dim _curTinhTitle = (From q In Data.DonViHanhChinhs Where q.Id = selectedTinhThanhId Select q.Title).FirstOrDefault
                _row(1) = _curTinhTitle
                curTinhThanh.Rows.Add(_row)
            End If


            With rptTinhThanhChinh
                .DataSource = curTinhThanh
                .DataBind()
            End With

        End Using

        '   Session("curTinh") = curTinhThanh



    End Sub
    Protected Sub rptTinhThanhChinh_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptTinhThanhChinh.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim lbtTitle As LinkButton = CType(e.Item.FindControl("lbtTitle"), LinkButton)


        lbtTitle.Text = e.Item.DataItem("Title")
        If e.Item.DataItem("Id") = ddlTinhThanh.SelectedValue Then
            lbtTitle.CssClass = "active"
        End If
        lbtTitle.ToolTip = e.Item.DataItem("Id")

    End Sub



    Private Sub BindToGrid(ByVal ipage As Integer, ByVal _TinhThanhId As Integer)
        Using Data As New VSEntities


            Dim count As Integer
            ' Dim a = (From r In Data.uspSelectSubCate(hidCateId.Value) Select r.CateID).ToArray

            'Dim cat = (From q In Data.Categories Where q.CatId = hidCateId.Value Select q).FirstOrDefault


            Dim p As List(Of TinMuaXe)

            p = (From q In Data.TinMuaXes Where q.TieuDe.Contains(txtTitle.Text) And (q.TinMuaXeKhoangGiaId = ddlKhoangGia.SelectedValue Or ddlKhoangGia.SelectedValue = 0) And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) And (q.TinhThanhId = _TinhThanhId Or _TinhThanhId = 0) _
                                            Order By q.Modified Descending, q.Created Descending Select q).Skip((ipage - 1) * 20).Take(20).ToList

            count = (From q In Data.TinMuaXes Where q.TieuDe.Contains(txtTitle.Text) And (q.TinMuaXeKhoangGiaId = ddlKhoangGia.SelectedValue Or ddlKhoangGia.SelectedValue = 0) And q.StatusId = 3 _
                                            And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                            Or q.eEffectiveDate Is Nothing) And (q.TinhThanhId = _TinhThanhId Or _TinhThanhId = 0) Select q.Id).Count
            'xem nhieu trong tuan

            lblTotal.Text = count
            lblTotal2.Text = count
            If count > 0 Then
                hidCount.Value = count
                Create_Pager(hidCount.Value, ipage, 20, 10)

            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If

            With rptProduct
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub

    Protected Sub rtpProduct_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProduct.ItemDataBound
        Dim row As VSModel.TinMuaXe = CType(e.Item.DataItem, VSModel.TinMuaXe)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        'Dim ltrTinhTrangXe As Literal = CType(e.Item.FindControl("ltrTinhTrangXe"), Literal)


        'ltrTinhTrangXe.Text = getTinhTrangXe(row.RaovatId)

        hplTitle.Text = row.TieuDe & " - " & row.TinMuaXeKhoangGia.Title
        '  hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.RaovatId.ToString & ".html"
        Dim ltrDescription As Literal = CType(e.Item.FindControl("ltrDescription"), Literal)
        ltrDescription.Text = CropText(row.NoiDung, 300, "...")
        'Dim ltrUSD As Literal = CType(e.Item.FindControl("ltrUSD"), Literal)
        'ltrUSD.Text = "~ " & CType((row.OriginalPrice * 1000000) / TyGia, Double).ToString("#,##0") & " USD"



        '  hplTitle.NavigateUrl = MainDomain & row.Store.QueryString & "-" & row.StoreId & "/" & row.QueryString & "-" & row.ProductId.ToString & "/chi-tiet.html"



        ' Dim imgProduct As Image = CType(e.Item.FindControl("imgProduct"), Image)

        '   imgProduct.ImageUrl = CookedImageURL("84-84", row.ImagePreview.ToString)

        ' Dim ltrPrice As Literal = CType(e.Item.FindControl("ltrPrice"), Literal)
        '     ltrPrice.Text = FormatCurrencyOto(row.OriginalPrice.ToString)

        Dim ltrLienHe As Literal = CType(e.Item.FindControl("ltrLienHe"), Literal)

        Dim ltrTelephone As Literal = CType(e.Item.FindControl("ltrTelephone"), Literal)
        ltrLienHe.Text = row.LienHe & "<br/>ĐC: " & row.DiaChi
        ltrTelephone.Text = row.SoDienThoai

        'If Not row.User.MobileOrther Is Nothing AndAlso row.User.MobileOrther <> "" Then
        '    ltrTelephone.Text = ltrTelephone.Text & " - " & row.User.MobileOrther
        'End If

        Dim ltrMaTin As Literal = CType(e.Item.FindControl("ltrMaTin"), Literal)
        ltrMaTin.Text = row.MaTin
        Dim ltrTinhThanh As Literal = CType(e.Item.FindControl("ltrTinhThanh"), Literal)
        If Not IsNothing(row.UserId) Then
            ltrTinhThanh.Text = GetTinhThanhPho(row.TinhThanhId)
        End If



    End Sub
    

    Private Function GetTinhThanhPho(ByVal _TinhThanhId As Integer) As String
        Using _data As New VSEntities
            Dim p = (From q In _data.DonViHanhChinhs Where q.Id = _TinhThanhId Select q.Title).FirstOrDefault
            If Not p Is Nothing Then
                Return p
            End If

        End Using
        Return ""
    End Function

    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='currentpage'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip, ddlTinhThanh.SelectedValue)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value, ddlTinhThanh.SelectedValue)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value, ddlTinhThanh.SelectedValue)
    End Sub


    Protected Sub ddlTinhThanh_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTinhThanh.SelectedIndexChanged
        BindToGrid(1, ddlTinhThanh.SelectedValue)
        TinhThanhChinh()
    End Sub
    Protected Sub lbtTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lbtTitle As LinkButton = CType(sender, LinkButton)
        ddlTinhThanh.SelectedValue = lbtTitle.ToolTip
        BindToGrid(1, lbtTitle.ToolTip)
        TinhThanhChinh()
    End Sub


    Protected Sub btnSearh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearh.Click
        BindToGrid(1, ddlTinhThanhSearh.SelectedValue)
    End Sub
End Class

